<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 1000px;
            height: 80px;
            position: relative;
        }

        ul {
            width: 1000px;
            height: 80px;
        }

        li {
            width: 200px;
            height: 80px;
            list-style: none;
            background-color: black;
            color: white;
            float: left;
            line-height: 80px;
            text-align: center;
            transition: .2s;
        }

        li:hover {
            background-color: #242323;
        }

        .cloud {
            width: 200px;
            height: 8px;
            background-color: #3d3c3c;
            position: absolute;
            left: 0;
            bottom: 0;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li>导航1</li>
            <li>导航2</li>
            <li>导航3</li>
            <li>导航4</li>
            <li>导航5</li>
        </ul>
        <div class="cloud"></div>
    </div>
    <script>
        // 获取所有的Li标签
        var lis = document.querySelectorAll('li')
        // 获取cloud标签
        var cloud = document.querySelector('.cloud')
        // 循环遍历li
        let num = 0
        for (let i = 0; i < lis.length; i++) {
            // 给每个li添加一个index属性方便后面使用
            lis[i].setAttribute('index', i)
            // 给li绑定鼠标移入事件
            lis[i].onmouseenter = function () {
                // 把cloud的位置移动到当前鼠标所在的li的下面
                cloud.style.left = 200 * i + 'px'
            }
            // 给li绑定鼠标移出事件
            lis[i].onmouseleave = function () {
                // 这里的num就是之前设置的index等下设置li的点击把index赋值给num
                cloud.style.left = 200 * num + 'px'
            }
            lis[i].onclick = function () {
                num = this.getAttribute('index')
                cloud.style.left = 200 * num + 'px'
            }
        }
    </script>
</body>

</html>